package com.layui.controller;

import static org.hamcrest.CoreMatchers.nullValue;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.layui.entity.Address;
import com.layui.result.ResultVo;
import com.layui.result.ResultVoUtil;
import com.layui.service.AddressService;

/**
 *
 * @author LiShuai
 * @date 2019年2月24日
 */
@Controller
@RequestMapping(value = "/address")
public class AddressController {

	@Autowired
	private AddressService addressService;

	/**
	 * 跳转到三级联动页面
	 * 
	 * @param model
	 * @return
	 */
	// 知识点：springmvc的 ant风格和@PathVariable
	@RequestMapping(value = "/three/{method}/{defaultAddress}", method = RequestMethod.GET)
	public String toThreeAjax(Model model, @PathVariable("method") String method,
			@PathVariable("defaultAddress") boolean defaultAddress) {

		System.out.println("AddressController.toThreeAjax()");

		// defaultAddress 如果 为true，则设置默认地址；如果为false，则不设置
		System.out.println("defaultAddress的值为：" + defaultAddress);
		if (defaultAddress) {
			model.addAttribute("provinceId", 1);
			model.addAttribute("cityId", 2);
			model.addAttribute("countyId", 14);
		}

		if ("method1".equals(method)) {
			System.out.println("====address1======AddressController.toThreeAjax()");
			return "level_3_linkage/address1";
		}
		if ("method2".equals(method)) {
			System.out.println("====address2======AddressController.toThreeAjax()");
			return "level_3_linkage/address2";
		}
		return null;
	}

	// -----------------------------------分开查询：开始--------------------------------------------------------

	/**
	 * 根据省id查询市，即根据市的pid查询
	 * 
	 * @param provincePid
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/findProvince", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo findProvinceByPid(String provincePid) {

		System.out.println("AddressController.findProvinceByPid()");
		List<Address> provinces;
		if (StringUtils.isEmpty(provincePid)) {
			provinces = new ArrayList<Address>();
		} else {
			provinces = addressService.findProvinceByPid(Long.parseLong(provincePid));
		}
		return ResultVoUtil.success(provinces);
	}

	/**
	 * 根据省id查询市，即根据市的pid查询
	 * 
	 * @param cityPid
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/findCity", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo findCityByPid(String cityPid) {
		List<Address> citys;
		if (StringUtils.isEmpty(cityPid)) {
			citys = new ArrayList<Address>();
		} else {
			citys = addressService.findCityByPid(Long.parseLong(cityPid));
		}
		return ResultVoUtil.success(citys);
	}

	/**
	 * 根据市id查询县/区，即根据县/区的pid查询
	 * 
	 * @param countyPid
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/findCounty", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo findCountyByPid(String countyPid) {
		System.out.println("AddressController.findCountyByPid()");
		List<Address> countys;
		if (StringUtils.isEmpty(countyPid)) {
			countys = new ArrayList<Address>();
		} else {
			countys = addressService.findCountyByPid(Long.parseLong(countyPid));
		}

		return ResultVoUtil.success(countys);
	}

	// -----------------------------------分开查询：结束--------------------------------------------------------

	/**
	 * 省市县三级查询整合
	 * 
	 * @param pid
	 * @return
	 */

	@RequestMapping(value = "/findAddress", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo<?> findAddressByPid(String pid) {

		System.out.println("AddressController.findAddressByPid()");

		List<Address> addresses;
		if (StringUtils.isEmpty(pid)) {
			addresses = new ArrayList<Address>();
		} else {
			addresses = addressService.findAddressByPid(Long.parseLong(pid));
		}
		return ResultVoUtil.success(addresses);
	}

}
